package com.gitee.wsl.statemachine.trigger

interface ActionTrigger<State>{

    fun tryAction(beforeState:State,toState: State):Boolean

    fun before(beforeState:State,toState: State){ }

    fun entry(beforeState:State,toState: State)

    fun after(beforeState:State,toState: State){}

    fun leave(beforeState:State,toState: State){}

}

class SampleActionTrigger<State>(val state: State, val handlerConfig: ActionTriggerConfig<State>): ActionTrigger<State> {

    override fun tryAction(beforeState: State, toState: State): Boolean {
        handlerConfig.tryActionHandler?.let {
            return it(beforeState, toState)
        }
        return true
    }

    override fun before(beforeState:State,toState: State) {
        handlerConfig.beforeHandler?.let {
            it(beforeState, toState)
        }
    }

    override fun entry(beforeState:State,toState: State) {
        handlerConfig.entryHandler?.let {
            it(beforeState, toState)
        }
    }

    override fun after(beforeState:State,toState: State) {
        handlerConfig.afterHandler?.let {
            it(beforeState, toState)
        }
    }

    override fun leave(beforeState:State,toState: State) {
       handlerConfig.leaveHandler?.let {
           it(beforeState, toState)
       }
    }
}